﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using PPIDataAccess;

namespace PPI.Data
{
    public static class DBHelper
    {
        /// <summary>
        /// Opens and returns a sql connection
        /// </summary>
        public static SqlConnection GetConnection(string connectionKey)
        {
            try
            {

                SqlConnection _Con = new SqlConnection(Database.Default[connectionKey].ToString()); //FIXME: ADD CONNECTION STRING
                //SqlConnection _Con = new SqlConnection(ConfigurationManager.AppSettings["CentralServerConnectionString"]); //FIXME: ADD CONNECTION STRING
                if (_Con.State != ConnectionState.Open)
                    _Con.Open();

                return _Con;
            }
            catch
            {
                throw;
            }
        }
        public static SqlParameter CreateParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            return new SqlParameter(parameterName, dbType, size, direction, isNullable, precision, scale, String.Empty, DataRowVersion.Default, value);
        }
        public static SqlParameter CreateParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, dbType, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateIntParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Int, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateBitParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Bit, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateDateTimeParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.DateTime, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateTextParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            if (value == null)
                return CreateParameter(parameterName, SqlDbType.Text, 0, direction, true, 0, 0, DBNull.Value);
            else
                return CreateParameter(parameterName, SqlDbType.Text, value.ToString().Length + 1, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Char, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateVarCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.VarChar, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateNVarCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.NVarChar, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateRealParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Real, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateDecimalParameter(string parameterName, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Decimal, 0, direction, isNullable, precision, scale, value);
        }
        public static SqlParameter CreateFloatParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Float, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateImageParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Image, size, direction, isNullable, 0, 0, value);
        }
    }
}
